package org.uk.splitsbrowser.server;

import java.util.ArrayList;

import java.util.List;

import org.uk.splitsbrowser.client.presenter.OEventService;
import org.uk.splitsbrowser.shared.OEvent;
import org.uk.splitsbrowser.shared.SplitsbrowserException;
import org.uk.splitsbrowser.shared.SplitsbrowserSecurityException;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.orientechnologies.orient.core.db.record.OIdentifiable;
import com.orientechnologies.orient.core.id.ORID;
import com.orientechnologies.orient.core.id.ORecordId;
import com.orientechnologies.orient.core.sql.query.OSQLSynchQuery;
import com.orientechnologies.orient.object.db.OObjectDatabaseTx;

public class OEventServiceImpl extends RemoteServiceServlet implements OEventService {
	private static final long serialVersionUID = -6624707416167247605L;

	/** Throw exception of we are not logged on to edit the event */
	private boolean checklogin(OEvent event) throws SplitsbrowserException {
		return true;
	}

	@Override
	public void saveEvent(OEvent event) throws SplitsbrowserException {

		// Check we are logged in with the correct user for the event
		checklogin(event);

		OObjectDatabaseTx db = DBService.getDB();
		try {
			db.attachAndSave(event);
		} finally {
			db.close();
		}
	}

	@SuppressWarnings("deprecation")
	@Override
	public String newEvent(OEvent event) throws SplitsbrowserException {

		// Verify we are logged in as some user before saving the event
		checklogin(null);

		// Save the event
		OObjectDatabaseTx db = DBService.getDB();
		try {
			event = db.save(event);
		} finally {
			db.close();
		}
		return event.getId();
	}

	@Override
	public void deleteEvent(String id) throws SplitsbrowserException {

		// Check we are logged in with the correct user for the event

		OObjectDatabaseTx db = DBService.getDB();
		try {
			ORecordId oid = new ORecordId(id);
			db.delete(oid);

			// Delete images

		} finally {
			db.close();
		}
	}

	@Override
	public List<OEvent> queryEvent(String queryString, int offset, int count) {

		OObjectDatabaseTx db = DBService.getDB();
		try {
			String sql;

			if (offset < 0 || offset > 10000) {

			}

			if (count < 0 || count > 10000) {

			}

			if (queryString == null || queryString.equals("")) {
				sql = "select * from OEvent ";
			} else {

				// Split the query string into a number of words and search for
				// any occurrence of them
				String[] words = queryString.split(" ");

				sql = "select * from OEvent where ";
				for (int i = 0; i < words.length - 1; i++) {
					sql = sql.concat("any() like '").concat(words[i]).concat("%' ");
				}
				sql = sql.concat("any() like '").concat(words[words.length - 1]).concat("%' ");
			}

			sql = sql.concat("order by date desc ");

			if (offset > 0) {
				sql = sql.concat("skip ").concat(Integer.toString(offset)).concat(" ");
			}

			if (count > 0) {
				sql = sql.concat("limit ").concat(Integer.toString(count));
			}

			List<OEvent> result1 = db.query(new OSQLSynchQuery<OEvent>(sql));

			List<OEvent> result = new ArrayList<OEvent>();

			// Detach the objects
			for (OEvent event : result1) {
				event = db.detachAll(event, true);
				result.add(event);
			}

			return (result);

		} finally {
			db.close();
		}
	}

	@Override
	public OEvent getEvent(String id) {

		OObjectDatabaseTx db = DBService.getDB();
		try {
			ORecordId oid = new ORecordId(id);
			OEvent event = db.load(oid);
			event = db.detach(event, true);
			return (event);
		} finally {
			db.close();
		}
	}

	@Override
	public void uploadResults(OEvent event, ResultsImportFormat format, String filename) {
		// TODO Auto-generated method stub

	}

	@Override
	public void uploadMap(OEvent event, String filename) {
		// TODO Auto-generated method stub

	}

	@Override
	public void uploadCourses(OEvent event, String filename) {
		// TODO Auto-generated method stub

	}

}
